package com.scalar.chiptrack.rma.actions;

import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.sales.dao.CustomerDAO;
import com.scalar.chiptrack.sales.dao.NewCustomerDAO;
import com.scalar.chiptrack.sales.forms.SelectCustomerForm;
import com.scalar.chiptrack.users.User;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;


/**
 * Implementation of <strong>Action</strong> that gives customers list,
 * search with customer name, create rma
 *
 * @author   Basha
 * @version $Revision: 1.3 $ $Date: 2007/08/28 14:10:13 $
 */

public final class SelectCustomerForRmaAction extends ChipTrackAction
{

    private static final String ACTION_NAME = "/selectCustomerForRma.do";

    // ----------------------------------------------------- Instance Variables
    private String customerName = null;
    private ArrayList customersList = new ArrayList ();
    // --------------------------------------------------------- Public Methods

    /**
     * Process the specified HTTP request, and create the corresponding HTTP
     * response (or forward to another web component that will create it).
     * Return an <code>ActionForward</code> instance describing where and how
     * control should be forwarded, or <code>null</code> if the response has
     * already been completed.
     *
     * @param mapping The ActionMapping used to select this instance
     * @param form The optional ActionForm bean for this request (if any)
     * @param request The HTTP request we are processing
     * @param response The HTTP response we are creating
     *
     * @exception Exception if business logic throws an exception
     */
    public ActionForward execute ( ActionMapping mapping,
                                   ActionForm form,
                                   HttpServletRequest request,
                                   HttpServletResponse response )
            throws Exception
    {

        SelectCustomerForm subform = ( SelectCustomerForm ) form;
        String action = request.getParameter ( "action" );


        HttpSession session = request.getSession ();
        User user = ( User ) session.getAttribute ( SessionKeys.USER_KEY );

        if ( user == null )
        {
            session.setAttribute ( SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME + "?" + request.getQueryString () );
            return ( mapping.findForward ( "login" ) );
        }

        HashMap errors = new HashMap ();
        if ( action == null )
            action = "SelectCustomer";

        if ( "SelectCustomer".equals ( action ) )
        {
            request.setAttribute ( "customers", NewCustomerDAO.getAllCustomerNamesByUserTypeBySetup( null, user, null ) );
            request.setAttribute ( "showList", "FALSE" );

            return ( mapping.findForward ( "customerList" ) );
        }

        if ( "DisplayCustomerSearch".equals ( action ) )
        {
            subform.setCustomerName ( "" );
            subform.setSelectedCustomer ( "0" );
            request.setAttribute ( "customers", NewCustomerDAO.getAllCustomerNamesByUserTypeBySetup( null, user, null ) );
            request.setAttribute ( "showList", "FALSE" );
            return ( mapping.findForward ( "customerList" ) );
        }

        if ( "SearchCustomer".equals ( action ) )
        {
            customerName = BeanUtils.getSimpleProperty ( form, "customerName" );
            String selectedCustomer = BeanUtils.getSimpleProperty ( form, "selectedCustomer" );

            //System.out.println("RMA Customer Id:"+selectedCustomer);

            if ( Integer.parseInt ( selectedCustomer ) == 0 )
            {
				customersList = CustomerDAO.getAllCustomers ( customerName, user);

                request.setAttribute ( "customersList", customersList );
                request.setAttribute ( "customers", NewCustomerDAO.getAllCustomerNamesByUserTypeBySetup( null, user, null ) );


                if ( customersList.size () == 0 ) {
                    errors.put ( ERROR_CUSTOMER_NOT_FOUND, null );
                } else {
                    request.setAttribute ( "showList", "TRUE" );
                }

                if ( !errors.isEmpty () ) {
                    request.setAttribute ( SessionKeys.ERROR_MESSAGES, errors );
                }

                return ( mapping.findForward ( "customerList" ) );
            }
            else
            {
                if(selectedCustomer ==  null){
				    //Throw error message need to implement
			    }

                request.setAttribute("customerId",selectedCustomer);

                return (mapping.findForward("success"));
            }

        }

        return ( mapping.findForward ( "customerList" ) );
    }
}
